package service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.MessageDao;
import dao.MessageMapper;
import pojo.User;
import pojo.Message;
import pojo.Essay;

@Service
@Transactional
public class MessageServiceImpl implements MessageServiceI {

    @Autowired
    private MessageDao messageDao;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserServiceI userServiceI;
    @Autowired
    private EssayServiceI essayServiceI;

    @Override
    public List<Message> FindAllMessage(Integer userId) {
        return messageDao.selectByUser(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean RemoveMessageByUserId(Integer userId, String flag) {
        if (flag == "all") {
            List<Integer> list = messageDao.selectAllMessageId(userId);
            try {
                for (Integer integer : list) {
                    messageMapper.deleteByPrimaryKey(integer);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean removeMessageByMessageId(Integer messageId) {
        int i = messageMapper.deleteByPrimaryKey(messageId);
        if (i > 0) {
            return true;
        } else
            return false;
    }

    @Override
    public boolean HaveNoReadMessage(Integer userId) {
        int read = 0;
        List<Message> list = messageDao.selectNotRead(userId, read);
        if (list.size() > 0) {
            return true;
        } else
            return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean MakeMessage(Integer suserId, Integer ruserId, Integer flag, Integer essayId) {
        /******************************* 获取系统时间 **************************************/
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置时间格式
        String today = df.format(new Date());
        /**********************************获取发起人的用户名**************************************************/
        String suserName = null;
        String title = null;
        if (suserId == -1) {
            suserName = "管理员";
        } else {
            User user = userServiceI.FindUserById(suserId);
            suserName = user.getUserName();// 获取消息发起人的姓名
            Essay essay = essayServiceI.FindEssay(essayId);
            title = essay.getTitle();
        }
        int notread = 0;
        /******************************************/
        if (flag == 0) {
            Message message = new Message();
            String messageContent = suserName + "  点赞     了你的文章      《" + title + "》";
            message.setMessageContent(messageContent);
            message.setMessageDate(today);
            message.setReadornot(notread);
            message.setRuserId(ruserId);
            message.setSuserId(suserId);
            try {
                messageMapper.insertSelective(message);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;

        } else if (flag == 1) {
            Message message = new Message();
            String messageContent = suserName + "  购买      了你的文章   《" + title + "》    的下载权限，积分已经存入你的仓库了";
            message.setMessageContent(messageContent);
            message.setMessageDate(today);
            message.setReadornot(notread);
            message.setRuserId(ruserId);
            message.setSuserId(suserId);
            try {
                messageMapper.insertSelective(message);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        } else if (flag == 2) {
            Message message = new Message();
            String messageContent = suserName + "  评论    了你的文章  《" + title + "》";
            message.setMessageContent(messageContent);
            message.setMessageDate(today);
            message.setReadornot(notread);
            message.setRuserId(ruserId);
            message.setSuserId(suserId);
            try {
                messageMapper.insertSelective(message);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        } else if (flag == 3) {
            Message message = new Message();
            String messageContent = suserName + "  收藏    了你的文章    《" + title + "》";
            message.setMessageContent(messageContent);
            message.setMessageDate(today);
            message.setReadornot(notread);
            message.setRuserId(ruserId);
            message.setSuserId(suserId);
            try {
                messageMapper.insertSelective(message);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean MakeMessageByManager(Integer suserId, Integer ruserId, Integer flag, Integer essayId, String context) {
        /******************************* 获取系统时间 **************************************/
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置时间格式
        String today = df.format(new Date());
        /**********************************获取发起人的用户名**************************************************/
        String suserName = null;
        if (suserId == -1) {
            suserName = "管理员";
        } else {
            User user = userServiceI.FindUserById(suserId);
            suserName = user.getUserName();// 获取消息发起人的姓名
        }
        Essay essay = essayServiceI.FindEssay(essayId);
        String title = essay.getTitle();
        int notread = 0;
        /******************************************/
        Message message = new Message();
        String messageContent = suserName + "删除  了你的文章    《" + title + "》，原因：<br>"+context;
        message.setMessageContent(messageContent);
        message.setMessageDate(today);
        message.setReadornot(notread);
        message.setRuserId(ruserId);
        message.setSuserId(suserId);
        try {
            messageMapper.insertSelective(message);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean MakeAttentionMessage(Message message) {
       int i= messageMapper.insertSelective(message);
        if (i > 0) {
            return true;
        }else
            return false;
    }

}
